package cn.geminis.data.jpa;

import cn.geminis.data.jpa.entity.*;
import cn.geminis.data.jpa.repository.ClassAdviserRepository;
import cn.geminis.data.jpa.repository.ClassRepository;
import cn.geminis.data.jpa.repository.StudentRepository;
import cn.geminis.data.jpa.repository.TeacherRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;
import java.util.ArrayList;

@Component
public class DataInit {

    @Autowired
    private StudentRepository studentRepository;

    @Autowired
    private TeacherRepository teacherRepository;

    @Autowired
    private ClassAdviserRepository classAdviserRepository;

    @Autowired
    private ClassRepository classRepository;

    public void init() {
        classAdviserRepository.deleteAll();
        teacherRepository.deleteAll();
        studentRepository.deleteAll();
        classRepository.deleteAll();

        var time = LocalDateTime.of(2018, 5, 30, 0, 0, 0);

        var students = new ArrayList<Student>();
        for (var i = 1; i <= 5; i++) {
            var student = new Student();
            student.setName("Student-" + i);
            student.setAge(i * 10);
            student.setBirthday(time);
            var photo = new Photo();
            photo.setPhotoData(new byte[]{1, 2, 3});
            photo.setPhotoData2(new byte[]{1, 2, 3});
            photo.setStudent(student);
            student.setPhoto(photo);

            for (var j = 1; j <= 2; j++) {
                var book = new Book();
                book.setName("Book-" + i + "-" + j);
                book.setStudent(student);
                student.getBooks().add(book);
            }

            students.add(student);
        }
        //因为设置了mappedBy，student由class管理，所以不用单独保存
        //studentRepository.saveAll(students);

        var classes = new ArrayList<Clazz>();
        for (var i = 1; i <= 2; i++) {
            var clazz = new Clazz();
            clazz.setName("Class-" + i);
            //class和student关系由class管理，只需class设置student，否则会重复插入中间表
            clazz.getStudents().addAll(students);

            var adviser = new ClassAdviser();
            adviser.setName("Class Adviser-" + i);
            adviser.setClazz(clazz);
            clazz.setAdviser(adviser);

            for (var j = 1; j <= 2; j++) {
                var teacher = new Teacher();
                teacher.setName("Teacher-" + j);
                teacher.getClasses().add(clazz);
                clazz.setTeacher(teacher);
            }

            classes.add(clazz);
        }
        //ManyToMany需要双向设置关系
        students.forEach(student -> student.getClasses().addAll(classes));
        classRepository.saveAll(classes);

        var student = new Student();
        student.setId("Student0");
        student.setName("Student-0");
        student.setBirthday(time);
        studentRepository.save(student);

        var clazz = new Clazz();
        clazz.setName("Class-0");
        classRepository.save(clazz);
    }
}
